home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / TUTORIALTOPIC.PY < prev    next >
Encoding:
Python Source  |  2000-06-29  |  8.8 KB  |  254 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. import OFS.Folder
  65. from HelpSys.HelpTopic import TextTopic
  66. from Globals import HTML, HTMLFile, MessageDialog
  67. import DateTime
  68. import DocumentTemplate
  69. import StructuredText
  70. import string, re
  71.  
  72.  
  73. pre_pat=re.compile(r'<PRE>(.+?)</PRE>', re.DOTALL)
  74. tutorialExamplesFile='ZopeTutorialExamples.zexp'
  75.  
  76. class TutorialTopic(TextTopic):
  77.     """
  78.     A Tutorial Help Topic
  79.     """
  80.  
  81.     def __init__(self, id, title, text):
  82.         self.id=id
  83.         self.title=title
  84.         text=str(StructuredText.HTML(text))
  85.         self.obj=HTML(pre_pat.sub(clean_pre, text))
  86.         
  87.     index_html=HTMLFile('lessonView', globals())
  88.             
  89.     def lessonURL(self, id, REQUEST):
  90.         """
  91.         URL of the examples for a lesson
  92.         """
  93.         try:
  94.             return '%s/lesson%d' % (REQUEST['tutorialExamplesURL'], id)
  95.         except KeyError:
  96.             return ""
  97.             
  98.     def tutorialShowLesson(self, id, REQUEST):
  99.         """
  100.         Navigate management frame to a given lesson's screen.
  101.         """
  102.         url=self.lessonURL(id, REQUEST)
  103.         if not url:
  104.             return """\
  105. <p class="warning">
  106. Zope cannot find the tutorial examples.
  107. You should install the tutorial examples before
  108. continuing. Choose "Zope Tutorial" from the product
  109. add list in the Zope management screen to install
  110. the examples.
  111. </p>
  112. <p class="warning">
  113. If you have already installed the tutorial, you can either
  114. follow along manually, or reinstall the tutorial examples.
  115. Note: make sure that you have cookies turned on in your browser.
  116. </p>
  117. """
  118.         
  119.         return """\
  120. <SCRIPT LANGUAGE="javascript">
  121. <!--
  122. window.open("%s/manage_main", "manage_main");
  123. //-->
  124. </SCRIPT>
  125. <p class="information">
  126. <a href="%s/manage_main" target="manage_main"
  127. onClick="javascript:window.open('%s/manage_main', 'manage_main').focus()"
  128. >Show lesson examples</a> in another window.
  129. </p>""" % (url, url, url)
  130.  
  131.     def apiLink(self, klass, REQUEST):
  132.         """
  133.         Returns the URL to a API documentation for a given class.
  134.         """
  135.         names=string.split(klass, '.')
  136.         url="%s/Control_Panel/Products/%s/Help/%s.py#%s" % (REQUEST['SCRIPT_NAME'],
  137.                 names[0], names[1], names[2])
  138.         return '<a href="%s">API Documentation</a>' % url
  139.  
  140.     tutorialNavigation=HTMLFile('tutorialNav', globals())
  141.  
  142.  
  143. addTutorialForm=HTMLFile('tutorialAdd', globals())
  144.  
  145. def addTutorial(self, id, REQUEST=None, RESPONSE=None):
  146.     """
  147.     Install tutorial examples.
  148.     """
  149.     id=str(id)
  150.     ob=OFS.Folder.Folder()
  151.     ob.id=id
  152.     ob.title='Zope Tutorial Examples'
  153.     id=self._setObject(id, ob)
  154.     folder=getattr(self, id)
  155.  
  156.     # make sure that Gadfly is initialized
  157.     try:
  158.         from Products.ZGadflyDA.DA import data_sources
  159.         data_sources()
  160.     except:
  161.         raise 'Bad Request', 'The ZGadflyDA product must be installed!'
  162.  
  163.     # work around old Zope bug in importing
  164.     try:
  165.         folder.manage_importObject(tutorialExamplesFile)
  166.     except:
  167.         folder._p_jar=self.Destination()._p_jar
  168.         folder.manage_importObject(tutorialExamplesFile)
  169.  
  170.     # acquire REQUEST if necessary
  171.     if REQUEST is None:
  172.         REQUEST=self.REQUEST
  173.         
  174.     # Set local roles on examples
  175.     changeOwner(folder, REQUEST['AUTHENTICATED_USER'])
  176.  
  177.     # Run lesson setup methods -- call Setup.setup methods in lesson folders
  178.     examples=folder.examples
  179.     for lesson in examples.objectValues():
  180.         if hasattr(lesson, 'Setup'):
  181.             lesson.Setup.setup(lesson.Setup, REQUEST)
  182.  
  183.     if RESPONSE is not None:
  184.         e=(DateTime.DateTime('GMT') + 365).rfc822()
  185.         RESPONSE.setCookie('tutorialExamplesURL', folder.absolute_url() + '/examples',
  186.                 path='/',
  187.                 expires=e)
  188.         RESPONSE.redirect(folder.absolute_url() + '/examples')
  189.  
  190.  
  191. def changeOwner(obj, owner):
  192.     """
  193.     Recursively changes the Owner of an object and all its subobjects.
  194.     """
  195.     for user, roles in obj.get_local_roles():
  196.         if 'Owner' in roles:
  197.             obj.manage_delLocalRoles([user])
  198.             break    
  199.     obj.manage_setLocalRoles(owner.getUserName(), ['Owner'])
  200.     for subobj in obj.objectValues():
  201.         changeOwner(subobj, owner)
  202.         
  203. def clean_pre(match):
  204.     """
  205.     Reformat a pre tag to get rid of extra indentation
  206.     and extra blank lines.
  207.     """
  208.     lines=string.split(match.group(1), '\n')
  209.     nlines=[]
  210.  
  211.     min_indent=None
  212.     for line in lines:
  213.         indent=len(line) - len(string.lstrip(line))
  214.         if min_indent is None or indent < min_indent:
  215.             if string.strip(line):
  216.                 min_indent=indent   
  217.     for line in lines:
  218.         nlines.append(line[min_indent:])
  219.     
  220.     while 1:
  221.         if not string.strip(nlines[0]):
  222.             nlines.pop(0)
  223.         else:
  224.             break
  225.     
  226.     while 1:
  227.         if not string.strip(nlines[-1]):
  228.             nlines.pop()
  229.         else:
  230.             break
  231.     
  232.     return "<PRE>%s</PRE>" % string.join(nlines, '\n')
  233.